<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="直播间名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入直播间名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开播时间" prop="startTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开播时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="下播时间" prop="endTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择下播时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="主播昵称" prop="anchorName">
        <el-input
          v-model="queryParams.anchorName"
          placeholder="主播昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信号" prop="anchorWechat">
        <el-input
          v-model="queryParams.anchorWechat"
          placeholder="主播微信号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="密码口令观看" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码口令观看"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入房间号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['content:live:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['content:live:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:live:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['content:live:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="liveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="回放视频地址"  :show-overflow-tooltip="true" align="center" prop="id" />
      <el-table-column label="直播间名称"  :show-overflow-tooltip="true" align="center" prop="name" />
      <el-table-column label="封面" :show-overflow-tooltip="true" align="center" prop="coverImg" />
      <el-table-column label="开播时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下播时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主播昵称"  :show-overflow-tooltip="true" align="center" prop="anchorName" />
      <el-table-column label="主播微信号" align="center" prop="anchorWechat" />
      <el-table-column label="分享照片" :show-overflow-tooltip="true" align="center" prop="shareImg" />
      <!-- <el-table-column label="直播类型 直播类型，1 推流 0 手机直播" align="center" prop="type" /> -->
      <!-- <el-table-column label="1：横屏 0：竖屏" align="center" prop="screenType" /> -->
      <!-- <el-table-column label="是否 关闭点赞 1 关闭" align="center" prop="closeLike" /> -->
      <!-- <el-table-column label="是否 关闭商品货架，1：关闭" align="center" prop="closeGoods" /> -->
      <!-- <el-table-column label="是否开启评论，1：关闭" align="center" prop="closeComment" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="密码口令观看"  :show-overflow-tooltip="true" align="center" prop="password" />
      <el-table-column label="置顶直播" align="center" prop="isTop" :formatter="isTopFormat" />
      <el-table-column label="直播详细描述"  :show-overflow-tooltip="true" align="center" prop="detail" />
      <el-table-column label="直播图片集合" :show-overflow-tooltip="true" align="center" prop="imageUrls" />
      <!-- <el-table-column label="是否检查0未检查1是检查" align="center" prop="isCheck" :formatter="isCheckFormat" /> -->
      <el-table-column label="房间号 微信返回" :show-overflow-tooltip="true" align="center" prop="roomId" />
      <el-table-column label="是否下架" align="center" prop="isOffonline" :formatter="isOffonlineFormat" />
      <el-table-column label="是否免费" align="center" prop="isFree" :formatter="isFreeFormat" />
      <el-table-column label="价格" align="center" prop="price" />
      <!-- <el-table-column label="封面" align="center" prop="poster" /> -->
      <el-table-column label="回放视频地址" :show-overflow-tooltip="true" align="center" prop="mediaUrl" />
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['content:live:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['content:live:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改直播间管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="直播间名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入直播间名称" />
        </el-form-item>
        <el-form-item label="封面" prop="coverImg">
          <el-input v-model="form.coverImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="开播时间" prop="startTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开播时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="密码口令观看"  prop="password">
          <el-input v-model="form.password" placeholder="请输入密码口令观看" />
        </el-form-item>
        <el-form-item label="直播详细描述" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入直播详细描述" />
        </el-form-item>
        <el-form-item label="直播图片集合" prop="imageUrls">
          <el-input v-model="form.imageUrls" placeholder="请输入直播图片集合" />
        </el-form-item>
        <!-- <el-form-item label="是否检查0未检查1是检查" prop="isCheck">
          <el-input v-model="form.isCheck" placeholder="请输入是否检查0未检查1是检查" />
        </el-form-item> -->
        <!-- <el-form-item label="房间号 微信返回" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房间号 微信返回" />
        </el-form-item> -->
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <!-- <el-form-item label="封面" prop="poster">
          <el-input v-model="form.poster" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <!-- <el-form-item label="回放视频地址" prop="mediaUrl">
          <el-input v-model="form.mediaUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLive, getLive, delLive, addLive, updateLive, exportLive } from "@/api/content/live";

export default {
  name: "Live",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 直播间管理表格数据
      liveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 置顶直播字典
      isTopOptions: [],
      // 是否检查0未检查1是检查字典
      isCheckOptions: [],
      // 是否下架字典
      isOffonlineOptions: [],
      // 是否免费字典
      isFreeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        startTime: undefined,
        endTime: undefined,
        anchorName: undefined,
        anchorWechat: undefined,
        shareImg: undefined,
        type: undefined,
        screenType: undefined,
        closeLike: undefined,
        closeGoods: undefined,
        closeComment: undefined,
        createTime: undefined,
        password: undefined,
        isTop: undefined,
        detail: undefined,
        imageUrls: undefined,
        isCheck: undefined,
        roomId: undefined,
        isOffonline: undefined,
        isFree: undefined,
        price: undefined,
        poster: undefined,
        mediaUrl: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "直播间名称不能为空", trigger: "blur" }
        ],
        coverImg: [
          { required: true, message: "封面不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开播时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "下播时间不能为空", trigger: "blur" }
        ],
        anchorName: [
          { required: true, message: "主播昵称不能为空", trigger: "blur" }
        ],
        anchorWechat: [
          { required: true, message: "主播微信号不能为空", trigger: "blur" }
        ],
        shareImg: [
          { required: true, message: "分享照片不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("video_is_top").then(response => {
      this.isTopOptions = response.data;
    });
    this.getDicts("video_is_check").then(response => {
      this.isCheckOptions = response.data;
    });
    this.getDicts("sys_on_status").then(response => {
      this.isOffonlineOptions = response.data;
    });
    this.getDicts("video_free").then(response => {
      this.isFreeOptions = response.data;
    });
  },
  methods: {
    /** 查询直播间管理列表 */
    getList() {
      this.loading = true;
      listLive(this.queryParams).then(response => {
        this.liveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 置顶直播字典翻译
    isTopFormat(row, column) {
      return this.selectDictLabel(this.isTopOptions, row.isTop);
    },
    // 是否检查0未检查1是检查字典翻译
    isCheckFormat(row, column) {
      return this.selectDictLabel(this.isCheckOptions, row.isCheck);
    },
    // 是否下架字典翻译
    isOffonlineFormat(row, column) {
      return this.selectDictLabel(this.isOffonlineOptions, row.isOffonline);
    },
    // 是否免费字典翻译
    isFreeFormat(row, column) {
      return this.selectDictLabel(this.isFreeOptions, row.isFree);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        coverImg: undefined,
        startTime: undefined,
        endTime: undefined,
        anchorName: undefined,
        anchorWechat: undefined,
        shareImg: undefined,
        type: undefined,
        screenType: undefined,
        closeLike: undefined,
        closeGoods: undefined,
        closeComment: undefined,
        createTime: undefined,
        password: undefined,
        isTop: undefined,
        detail: undefined,
        imageUrls: undefined,
        isCheck: undefined,
        roomId: undefined,
        isOffonline: undefined,
        isFree: undefined,
        price: undefined,
        poster: undefined,
        mediaUrl: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加直播间管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLive(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改直播间管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateLive(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addLive(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除直播间管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLive(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有直播间管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportLive(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>